﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace LabMS.Web.User
{
    public partial class UserDefault : Base.BasePage
    {
        ExtendBLL.UserTable EBuserT = new ExtendBLL.UserTable();

        protected void Page_Load(object sender, EventArgs e)
        {
            strPageUserType.Add("mana");
            strPagePrivilege.Add("YHGL");
            if (!IsPostBack)
            {
                Bind();
                cancleUser.Attributes.Add("onclick", "return confirm('你确定要删除？');");
            }
        }

        /// <summary>
        /// 绑定
        /// </summary>
        protected void Bind()
        {
            DataSet ds = new DataSet();
            ds = EBuserT.GetAllList();//查出所有用户信息列表

            ds = HandleData(ds);

            gvuser.DataSource = ds.Tables[0].DefaultView;
            gvuser.DataBind();
        }

        protected DataSet HandleData(DataSet ds)
        {
            ds.Tables[0].Columns.Add("RealName");

            string PCode = "";
            string RealName = "";
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                PCode = ds.Tables[0].Rows[i]["PCode"].ToString();
                RealName = EBuserT.SerachRealName(PCode);
                if (RealName == "" || RealName == null)
                {
                    RealName = "暂无真实姓名";
                }
                ds.Tables[0].Rows[i]["RealName"] = RealName;   //把老师名字加入ds中
            }
            return ds;
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvuser_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvuser.PageIndex = e.NewPageIndex;
            Bind();
        }


        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //protected void addUser_Click(object sender, EventArgs e)
        //{
        //    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script type='text/javascript'>window.open('Add.aspx','','top=200,left=250,height=400,width=500');</script>");
        //}

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void cancleUser_Click(object sender, EventArgs e)
        {
            CheckBox chk;
            bool success = false;
            LabMS.BLL.UserTable BuserTable = new LabMS.BLL.UserTable();
            for (int i = 0; i < gvuser.Rows.Count; i++)
            {
                chk = (CheckBox)gvuser.Rows[i].FindControl("chkchoose");
                if (chk.Checked)
                {
                    int UserId = int.Parse(gvuser.DataKeys[i].Value.ToString());
                    try
                    {
                        BuserTable.Delete(UserId);
                        success = true;
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }
            if (success == true)
            {
                Common.JShelper.JSAlert(this.Page, "", "删除用户信息成功！");
                Bind();
            }
            else
            {
                Common.JShelper.JSAlert(this.Page, "", "删除用户信息失败！");
            }
        }

        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void search_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbusernameSearch.Text.Trim()))
            {
                Common.JShelper.JSAlert(this.Page, "", "姓名不能为空！");
                tbusernameSearch.Focus();
            }
            else
            {
                string str = "UserName like '%" + tbusernameSearch.Text.Trim() + "%'";
                DataSet ds = new DataSet();
                ds = EBuserT.GetList(str);
                HandleData(ds);
                gvuser.DataSource = ds.Tables[0].DefaultView;
                gvuser.DataBind();
            }
        }

        protected void gvuser_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor='White';this.style.color='#003399'");
                //e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor='#6699FF';this.style.color='#8C4510'");
                e.Row.Attributes["style"] = "Cursor:pointer";
                int itemp = e.Row.Cells.Count;
                for (int i = 1; i < itemp; i++)
                {
                    //e.Row.Cells[i].Attributes.Add("OnDblClick", "window.location.href='EditUser.aspx?UserID=" + gvuser.DataKeys[e.Row.DataItemIndex].Value.ToString() + "'");
                    e.Row.Cells[i].Attributes.Add("OnDblClick", "window.location.href='EditUser.aspx?UserID=" + gvuser.DataKeys[gvuser.DataKeys.Count - 1].Value.ToString() + "'");
                }
            }
        }
    }
}
